﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Modules.peddler {
    import flash.events.*;
    import Events.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import Display.*;
    import flash.text.*;
    import Classes.inventory.*;
    import Display.hud.components.*;
    import Display.peddlerUI.peddlerMarketUI.data.*;
    import Display.peddlerUI.*;
    import Engine.*;

    public class PeddlerGroupManager {

        public static const HAS_PLACEE_PEDDLER:String = "has_placed_peddler";
        public static const NO_SPACE_TO_PLACE_PEDDLER:String = "no_space_to_place_peddler";
        public static const NO_GOODS_TO_PLACE_PEDDLER:String = "no_goods_to_place_peddler";

        protected var m_isInitialized:Boolean = false;
        private var m_peddlers:Array;
        private var m_pdlrData:Object;
        public var hostPeddlers:Object;
        public var ownPeddlers:Object;
        public var hostCapacity:int = 0;
        public var selectPeddler:PeddlerData;
        private var m_cryOutMessage:String;
        private var m_selectedPeddler:MapResource;
        private var m_newItemData:Object;
        private var m_modleArray:Array;
        private var m_level:int = 0;
        private var m_totalHarvestNums:int = 0;
        private var m_loadOwnPeddlerFormServer:Boolean = false;

        public function PeddlerGroupManager(){
            this.m_modleArray = [];
            super();
        }
        public function loadObject(_arg1:Object):void{
            this.loadHostPeddlerData(_arg1);
            this.loadOwnPeddlerData(_arg1);
            this.initPeddlerGroupManager();
        }
        private function loadHostPeddlerData(_arg1:Object):void{
            this.hostPeddlers = _arg1.hostPeddlers;
            this.hostCapacity = _arg1.hostCapacity;
            this.m_pdlrData = this.hostPeddlers;
        }
        private function loadOwnPeddlerData(_arg1:Object):void{
            this.ownPeddlers = _arg1.ownPeddlers;
            this.m_totalHarvestNums = _arg1.totalHarvestNums;
            this.m_level = _arg1.gateUnlockLevel;
        }
        public function refreshOwnPeddlerData(_arg1:Object):void{
            this.selectPeddler = null;
            this.hideFriendBarPeddlerIcon();
            this.loadOwnPeddlerData(_arg1);
            this.initModel();
            this.addPeddlerIconFriendBar();
            this.reflashPeddlerCenterStatue();
        }
        public function initPeddlerGroupManager():void{
            this.initModel();
            this.cleanUp();
            this.drawView();
            this.m_isInitialized = true;
        }
        protected function drawView():void{
            var _local1:String;
            var _local2:Peddler;
            var _local3:PeddlerData;
            this.m_peddlers = Global.world.getObjectsByClass(Peddler);
            for (_local1 in this.m_pdlrData) {
                _local2 = (Global.world.getObjectById(int(_local1)) as Peddler);
                if (_local2){
                    _local3 = new PeddlerData(this.m_pdlrData[_local1].allowed, this.m_pdlrData[_local1].name, this.m_pdlrData[_local1].ownerId, this.m_pdlrData[_local1].peddlerId, this.m_pdlrData[_local1].state, this.m_pdlrData[_local1].timestamp);
                    _local2.peddlerModel = _local3;
                    _local2.setPick(this.createLotSlidePick(_local2));
                };
            };
        }
        private function createLotSlidePick(_arg1:Peddler):PeddlerSlidePick{
            var _local2:PeddlerSlidePick = new PeddlerSlidePick(this, _arg1, true);
            return (_local2);
        }
        private function drawVisitView():void{
        }
        public function initialize():void{
        }
        public function cleanUp():void{
            var _local1:uint;
            var _local2:Peddler;
            if (this.m_peddlers){
                _local1 = 0;
                while (_local1 < this.m_peddlers.length) {
                    _local2 = this.m_peddlers[_local1];
                    _local2.removePick();
                    _local2 = null;
                    _local1++;
                };
            };
        }
        protected function redrawView():void{
            this.drawView();
        }
        public function displayCreateOutcryDialog(_arg1:MapResource, _arg2:Object):void{
            this.m_selectedPeddler = _arg1;
            this.m_newItemData = _arg2;
            this.m_cryOutMessage = "";
            Utilities.setFullScreen(false);
            var _local3:PeddlerInputDialog = new PeddlerInputDialog("", "", 0, this.onCreateOutcryComplete, "PeddlerInput");
            _local3.textArea.addEventListener(Event.CHANGE, this.onCryoutDialogChange);
            UI.displayPopup(_local3);
        }
        private function onCryoutDialogChange(_arg1:Event):void{
            var _local2:TextField = (_arg1.target as TextField);
            if (_local2){
                this.m_cryOutMessage = _local2.text;
                this.m_cryOutMessage = this.m_cryOutMessage.replace(/[\r\n]/g, "");
                this.m_cryOutMessage = (this.m_cryOutMessage + ".");
            };
        }
        private function onCreateOutcryComplete(_arg1:GenericPopupEvent):void{
            var _local5:TPlacePeddlerAtFakeUser;
            var _local6:TPlacePeddler;
            var _local2:Peddler = (this.m_selectedPeddler as Peddler);
            var _local3:PeddlerData = (this.m_newItemData as PeddlerData);
            _local3.counting_time = GlobalEngine.getTimer();
            _local2.peddlerId = _local3.peddlerId;
            _local2.peddlerModel = _local3;
            var _local4:PeddlerSlidePick = this.createLotSlidePick(_local2);
            if (this.m_cryOutMessage.length == 0){
                UI.displayMessage(ZLoc.t("Dialogs", "peddler_no_input"));
            } else {
                this.selectPeddler = null;
                if (Global.getVisiting() == Player.FAKE_USER_ID_STRING){
                    _local5 = new TPlacePeddlerAtFakeUser(this.m_selectedPeddler, this.m_cryOutMessage, _local4);
                    GameTransactionManager.addTransaction(_local5);
                } else {
                    _local6 = new TPlacePeddler(this.m_selectedPeddler, this.m_cryOutMessage, _local4);
                    GameTransactionManager.addTransaction(_local6);
                };
                this.m_peddlers.push(_local2);
                _local2.addSupply("goods", int(_local3.item.commodityReq));
            };
        }
        public function canPlacePeddler(_arg1:String, _arg2:int=1):String{
            var _local6:Peddler;
            var _local7:Item;
            var _local8:int;
            var _local3:int;
            var _local4:int;
            var _local5:Array = Global.world.getObjectsByClass(Peddler);
            for each (_local6 in _local5) {
                if (_local6){
                    _local4++;
                    if (((_local6.peddlerModel) && ((_local6.peddlerModel.ownerId == Global.player.uid)))){
                        _local3++;
                    };
                };
            };
            if (_local3 >= _arg2){
                return (HAS_PLACEE_PEDDLER);
            };
            if (_local4 >= this.hostCapacity){
                return (NO_SPACE_TO_PLACE_PEDDLER);
            };
            _local7 = Global.gameSettings.getItemByName(_arg1);
            _local8 = Global.player.commodities.getCount(Commodities.GOODS_COMMODITY);
            if (_local8 < int(_local7.commodityReq)){
                return (NO_GOODS_TO_PLACE_PEDDLER);
            };
            return (null);
        }
        public function hasPlacePeddler(_arg1:String):PeddlerData{
            var _local2:PeddlerData;
            for each (_local2 in this.m_modleArray) {
                if (_local2.hostId == _arg1){
                    return (_local2);
                };
            };
            return (null);
        }
        private function initModel():void{
            var _local1:String;
            var _local2:Object;
            var _local3:PeddlerData;
            this.loadOwnPeddlerFormServer = false;
            this.m_modleArray = [];
            for (_local1 in this.ownPeddlers) {
                this.loadOwnPeddlerFormServer = true;
                _local2 = this.ownPeddlers[_local1];
                if (_local2){
                    _local3 = new PeddlerData(_local2.allowed, _local2.name, Global.player.uid, _local1, _local2.state, _local2.timestamp, _local2.hostId);
                    this.m_modleArray.push(_local3);
                };
            };
            this.m_modleArray = this.m_modleArray.sortOn("peddlerId");
        }
        private function addPeddlerIconFriendBar():void{
            var _local1:PeddlerData;
            var _local2:String;
            for each (_local1 in this.m_modleArray) {
                _local2 = _local1.hostId;
                if (_local2 == "0"){
                } else {
                    Global.ui.m_friendBar.addPeddlerIconForFriend(_local2);
                };
            };
        }
        private function hideFriendBarPeddlerIcon():void{
            var _local1:PeddlerData;
            var _local2:String;
            for each (_local1 in this.m_modleArray) {
                _local2 = _local1.hostId;
                if (_local2 == "0"){
                } else {
                    Global.ui.m_friendBar.hidePeddlerIcon(_local2);
                };
            };
        }
        public function getModelById(_arg1:String):PeddlerData{
            var _local2:PeddlerData;
            for each (_local2 in this.m_modleArray) {
                if (_local2.peddlerId == _arg1){
                    return (_local2);
                };
            };
            return (null);
        }
        public function get peddlerCenterCurrentLevel():int{
            return (this.m_level);
        }
        public function get successTimes():int{
            return (this.m_totalHarvestNums);
        }
        public function reflashPeddlerCenterStatue():void{
            var _local2:Municipal;
            if (Global.isVisiting()){
                return;
            };
            var _local1:Array = Global.world.getObjectsByNames(["mun_peddlercenter"]);
            for each (_local2 in _local1) {
                _local2.updateStagePickEffect();
            };
        }
        public function findDonutHungryPeddler():PeddlerData{
            var _local2:PeddlerData;
            var _local1:int;
            while (_local1 < this.m_modleArray.length) {
                _local2 = this.m_modleArray[_local1];
                if (_local2.hasFood == false){
                    return (_local2);
                };
                _local1++;
            };
            return (null);
        }
        public function wakeUpPeddlerData(_arg1:PeddlerData, _arg2:String=""):PeddlerData{
            var _local3:PeddlerData;
            for each (_local3 in this.m_modleArray) {
                if (_local3.peddlerId == _arg1.peddlerId){
                    _local3.status = HarvestableResource.STATE_PLOWED;
                    return (_local3);
                };
            };
            return (null);
        }
        public function hasPeddlers():Boolean{
            return ((((this.m_modleArray.length > 0)) && (this.m_loadOwnPeddlerFormServer)));
        }
        public function get peddlers():Array{
            var _local1:Item;
            var _local2:int;
            var _local3:int;
            var _local4:PeddlerData;
            if (((this.m_modleArray) && ((this.m_modleArray.length == 0)))){
                _local1 = Global.gameSettings.getItemByName("pdlr_lv1");
                _local2 = int(_local1.xml.allowAmount);
                _local3 = 0;
                while (_local3 < _local2) {
                    _local4 = new PeddlerData(false, _local1.name);
                    this.m_modleArray.push(_local4);
                    _local3++;
                };
            };
            return (this.m_modleArray);
        }
        private function set loadOwnPeddlerFormServer(_arg1:Boolean):void{
            var _local2:HUDComponent;
            this.m_loadOwnPeddlerFormServer = _arg1;
            if (Global.isVisiting()){
                _local2 = (Global.hud.getComponent("visitStack") as HUDComponentContainer).getComponent(HUDPeddlerComponent);
                if (_local2){
                    _local2.visible = _arg1;
                };
            };
        }
        public function addDonutsFromZMC():void{
            var _local1:PeddlerData = this.findDonutHungryPeddler();
            if (_local1){
                this.wakeUpPeddlerData(_local1);
            };
        }

    }
}//package Modules.peddler 
